<template>
  <el-dialog :visible="showDialog" title="新增员工" @close="cancel">
    <el-form
      ref="formData"
      label-width="120px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择聘用形式"
        >
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @focus="selectDepart"
        ></el-input>
        <!-- 防止一个tree组件 -->
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="list"
          :props="{ label: 'name' }"
          default-expand-all
          @node-click="selectNode"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择日期"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-button size="small" @click="cancel">取消</el-button>
      <el-button size="small" type="primary" @click="submitForm">
        确定
      </el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import employees from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      employees,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名长度为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        // 部门名称校验的trigger不能为blur，因为点击树形组件时就失去焦点了，触发验证，会一直显示为空
        departmentName: [
          { required: true, message: '部门名不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '转正时间不能为空', trigger: 'blur' }
        ]
      },
      list: [],
      loading: false,
      showTree: false
    }
  },
  methods: {
    async selectDepart() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      this.list = tranListToTreeData(depts, '')
      this.loading = false
    },
    selectNode(node) {
      this.formData.departmentName = node.name
      this.showTree = false
    },
    async submitForm() {
      // 校验表单
      try {
        await this.$refs.formData.validate()
        // 发请求
        await addEmployee(this.formData)
        // 关闭弹层 - this.$parent是父组件的实例对象，可以获得父组件身上的一切方法  - 使用条件：该子组件在父组件中，不是在其他element-ui的组件标签中使用的 - 也可以直接$emit调用父组件中的方法，修改page的话还是调用父组件中的方法比较好
        this.$parent.showDialog = false
        // 重新请求获取员工数据
        this.$parent.getEmployeesList()
        this.$parent.page.page = Math.ceil(
          this.$parent.total / this.$parent.page.size
        )
      } catch (error) {
        console.log(error)
      }
    },
    cancel() {
      this.$emit('update:showDialog', false)
      this.$refs.formData.resetFields()
    }
  }
}
</script>

<style></style>
